//
//  SetWeekTime.m
//  timelyRain
//
//  Created by Jiafei Wei on 16/8/8.
//  Copyright © 2016年 Jiafei Wei. All rights reserved.
//

#import "SetWeekTime.h"
#import "WeeKCell.h"
#import "MBTUiColor.m"
@interface SetWeekTime ()<UITableViewDataSource,UITableViewDelegate>{
    NSArray *dataName;
    NSIndexPath *last;
}
@property (weak, nonatomic) IBOutlet UITableView *doWeek;

@end

@implementation SetWeekTime

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor colorwithHexString:@"#F4F3F8"]];
    self.title =@"执行周期";
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@""style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = backItem;
    self.navigationController.navigationBarHidden=NO;
    _doWeek.rowHeight =40;
//    _doWeek.separatorColor = [UIColor redColor];
//    _doWeek.separatorInset = UIEdgeInsetsMake(1,0, 1,0); // 设置端距，这里表示separator离左边和右边均80像素
    last =[NSIndexPath indexPathForRow:0 inSection:0];
    _doWeek.dataSource =self;
    _doWeek.delegate=self;
    dataName =@[@"一次",@"每天",@"每周",@"每两周",@"每月",@"每年"];
    [_doWeek scrollsToTop];
}


//3 实现数据源的方法
#pragma mark - tableView数据源的方法
//返回每组多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataName.count;//个数
}
//返回每行显示的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //1 创建可重用的自定义的cell
    WeeKCell *cell = [WeeKCell setWeekTimeCellWithTableView:tableView];
    //2 设置cell内部的子控件
    NSString *name = dataName[indexPath.row];
    cell.name = name;
    if (indexPath.row==0) {
        cell.selcect =YES;
    }
    //3 返回
    return cell;
}

//当选中cell的时候执行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//获取当前的cell
  //单选的
    NSInteger currentRow =indexPath.row;
    if (currentRow!=last.row) {
        //不相等才会要改变
        WeeKCell *cell = [self.doWeek cellForRowAtIndexPath:indexPath];
        cell.selcect=YES;
        WeeKCell *lastCell = [self.doWeek cellForRowAtIndexPath:last];//把cell改变
        lastCell.selcect=NO;
        last=indexPath;//在重新给值
    }
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"";
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];
    NSUserDefaults *dd =[NSUserDefaults standardUserDefaults];
    [dd setObject:dataName[last.row] forKey:@"week"];
    [dd synchronize];
}
@end
